package com.itheima.web.Request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;

@WebServlet("/req2")
public class RequestDemo2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //GET请求逻辑
        //System.out.println("get.......");

        //1.获取所有参数的Map集合
        Map<String, String[]> map = req.getParameterMap();
        /* iter:快捷提示遍历*/
        for (String key : map.keySet()) {
            // username:zhangsan
            System.out.print(key+":");

            //获取值
            String[] values = map.get(key);
            for (String value : values) {
                System.out.print(value+" ");
            }
            System.out.println();
        }

        System.out.println("-----------------");


        //2.根据key获取对应的参数值，数组
        String[] hobbies = req.getParameterValues("hobby");

        for (String hobby : hobbies) {
            System.out.println(hobby);
        }

        //3.根据key，获取单个的参数值
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username);
        System.out.println(password);


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //POST请求逻辑
        this.doGet(req,resp);


   /*     System.out.println("post.......");

        //1.获取所有参数的Map集合
        Map<String, String[]> map = req.getParameterMap();
        *//* iter:快捷提示遍历*//*
        for (String key : map.keySet()) {
            // username:zhangsan
            System.out.print(key+":");

            //获取值
            String[] values = map.get(key);
            for (String value : values) {
                System.out.print(value+" ");
            }
            System.out.println();
        }

        System.out.println("-----------------");


        //2.根据key获取对应的参数值，数组
        String[] hobbies = req.getParameterValues("hobby");

        for (String hobby : hobbies) {
            System.out.println(hobby);
        }

        //3.根据key，获取单个的参数值
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username);
        System.out.println(password);

*/
    }
}
